import fse from 'fs-extra';
import path from 'path';

import { buildAppAndHapsAndCheckFile } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.EmptyAbility]
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  // 1. 删除entry和application模块下的ohosTest目录
  for (const moduleName of project.getModuleNames()) {
    const ohosTestPath = path.resolve(project.getModule(moduleName).moduleRootPath(), 'src', 'ohosTest');
    if (await fse.pathExists(ohosTestPath)) {
      await fse.remove(ohosTestPath);
    }
  }

  // 2. 执行build haps和build app
  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_AIDE_ZOS_OHOS_BUILD_HAP_ohosTest_StageMode_api10_08')
  .description('8.新模型entry/feature删除ohosTest目录构建')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.defaultOpt())
  .testFun(testFun)
  .load();
